package com.example.administrator.readbook.greendao;

import com.example.administrator.readbook.MyApplication;

import java.util.List;

/**
 * Created by Administrator on 2018/7/6.
 */

public class BookDao {
    public static void insertBook(GreenDaoBookBean bookBean) {
        MyApplication.getDaoInstance().getGreenDaoBookBeanDao().insertOrReplace(bookBean);
    }

    public static void deleteBook(String bookId) {
//        GreenDaoBookBeanDao bookBeanDao = MyApplication.getDaoInstance().getGreenDaoBookBeanDao();
//        bookBeanDao.getDatabase().execSQL("delete from GREEN_DAO_BOOK_BEAN where bookId = " + bookId);
        MyApplication.getDaoInstance().getGreenDaoBookBeanDao().deleteByKey(bookId);
    }

    public static void deleteBook(GreenDaoBookBean bookBean) {
        MyApplication.getDaoInstance().getGreenDaoBookBeanDao().delete(bookBean);
    }

    public static void updateBook(String bookName,String readChapterName) {
        GreenDaoBookBean bookBean = query(bookName);
        bookBean.setBookReadChapter(readChapterName);
        MyApplication.getDaoInstance().getGreenDaoBookBeanDao().update(bookBean);
    }

    public static GreenDaoBookBean query(String bookName) {
        return MyApplication.getDaoInstance().getGreenDaoBookBeanDao().queryBuilder().where(GreenDaoBookBeanDao.Properties.BookName.eq(bookName)).unique();
    }

    public static List<GreenDaoBookBean> query() {
        return MyApplication.getDaoInstance().getGreenDaoBookBeanDao().loadAll();
    }
}
